package com.example.campus.api_controller;


import com.example.campus.dao.table_gateway.CourseMapper;
import com.example.campus.dao.model.Course;
import com.example.campus.page_controller.dto.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.example.campus.common.BusinessConstants.API_BASE;
import static com.example.campus.page_controller.dto.ResponseResult.ok;

@RestController
@RequestMapping(API_BASE + "/courses")
public class CourseController {
    @Autowired
    CourseMapper courseMapper;


    @GetMapping()
    public ResponseResult<List<Course>> getCourses() {
        var courses =  courseMapper.allCourses();
        return ResponseResult.ok(courses);
    }

    @PostMapping()
    public ResponseResult<String> addCourse(@RequestBody Course course) {
        course.setCreatedTime(new Date());
        courseMapper.addCourse(course);

        return ResponseResult.ok();
    }

    @GetMapping("/{id}")
    public  ResponseResult<Course> getCourseById(@PathVariable("id") long id){
        var course = courseMapper.courseById(id);
        if(course == null){
            return  ResponseResult.notFound();
        }
        return  ResponseResult.ok(course);
    }


}
